let interval;

Page({
  data: {
    text: '登陆后可查看温馨提示和近日限行规则',
    marWidth: 0,
    mainWidth: 0,
    move: 70,
    timer: "",
    banners: [], // 用于存储轮播图图片
    today_limit1: "-",
    today_limit2: "-",
    tomorrow_limit1: "-",
    tomorrow_limit2: "-",
    today_limit_date1: "",
    today_limit_date2: "",
    tomorrow_limit_date1: "",
    tomorrow_limit_date2: "",
    
  },

  onShow: function(options) {
    // 登录次数统计
    wx.request({
      url: 'https://api.kuangqiaodongjie.cn/user/UserFreq/',
      method: 'POST',
      header: {
        'Authorization': wx.getStorageSync('token') // 使用本地存储的 token
      },
      success: res => {
        console.log("登录次数+1");
        console.log(res.data.code);
        if (res.data.code === 200) {
          this.setData({ login: true });
        } else {
          this.setData({ login: false });
        }
        console.log(login);
      }
    });

    // 获取限行信息
    this.getLimitData();
    // 获取温馨提示
    this.getWarnText();
    // 获取轮播图数据
    this.getBannerData(); // 登录后获取轮播图数据
  },

  // 获取今天和明天的日期
  getFormattedDate: function(offset) {
    const today = new Date();
    today.setDate(today.getDate() + offset); // offset 0 是今天，1 是明天
    const year = today.getFullYear();
    const month = today.getMonth() + 1; // 月份从0开始，所以要加1
    const day = today.getDate();
    return `${year}年${month < 10 ? '0' + month : month}月${day < 10 ? '0' + day : day}号`;
  },

  // 获取限行数据
  getLimitData: function() {
    wx.request({
      url: 'https://api.kuangqiaodongjie.cn/user/LimitFinal/',
      method: 'GET',
      header: {
        'Authorization': wx.getStorageSync('token') // 使用本地存储的 token
      },
      success: (res) => {
        console.log("限行data", res.data);
        if (res.data.code != 419) {
          this.setData({
            today_limit1: res.data.data.today_limit1,
            today_limit2: res.data.data.today_limit2,
            tomorrow_limit1: res.data.data.tomorrow_limit1,
            tomorrow_limit2: res.data.data.tomorrow_limit2,
            // 动态设置今天和明天的日期
            today_limit_date1: this.getFormattedDate(0), // 今天的日期
            today_limit_date2: this.getFormattedDate(0),
            tomorrow_limit_date1: this.getFormattedDate(1), // 明天的日期
            tomorrow_limit_date2: this.getFormattedDate(1)
          });
        }
      },
      fail: (res) => {
        console.log("error", res.data);
      }
    });
  },

  // 获取温馨提示
  getWarnText: function() {
    wx.request({
      url: 'https://api.kuangqiaodongjie.cn/user/WarnUser/',
      method: 'GET',
      header: {
        'Authorization': wx.getStorageSync('token') // 使用本地存储的 token
      },
      success: (res) => {
        console.log(111, res.data);
        if (res.data.code != 419) {
          if (res.data.data.warn_text) {
            this.setData({
              text: res.data.data.warn_text
            });
          } else {
            this.setData({
              text: '温馨提示获取失败'
            });
          }
        }
      },
      fail: (res) => {
        console.log(res.data);
      }
    });
  },

  // 获取轮播图数据
  getBannerData: function() {
    wx.request({
      url: 'https://api.kuangqiaodongjie.cn/user/CoverUser/',
      method: 'GET',
      header: {
        'Authorization': wx.getStorageSync('token') // 使用本地存储的 token
      },
      success: (res) => {
        console.log(res.data);
        if (res.data && res.data.data) {
          const banners = res.data.data.map(item => `https://api.kuangqiaodongjie.cn${item.cover_file}`);
          this.setData({
            banners: banners
          });
        }
      },
      fail: (res) => {
        console.error("获取轮播图失败", res);
      }
    });
  },

  // 初始化跑马灯
  initMarquee() {
    let query = wx.createSelectorQuery();
    query.select('.marquee').boundingClientRect((res) => {
      this.data.marWidth = parseInt(res.width);
    }).exec();
    query.select('.main').boundingClientRect((res) => {
      this.data.mainWidth = parseInt(res.width) - 10;
      this.moveText();
    }).exec();
  },

  // 移动跑马灯文字
  moveText() {
    this.data.timer = setInterval(() => {
      this.setData({
        move: this.data.move - 2
      });
      if (parseInt(this.data.move) <= -this.data.mainWidth) {
        this.setData({
          move: this.data.marWidth
        });
        clearInterval(this.data.timer);
        this.moveText();
      }
    }, 80);
  },

  // 问题建议
  suggestion: function() {
    if (!this.data.login) {
      wx.showModal({
        title: '提示',
        content: '请先完成登录',
        success: (res) => {
          if (res.confirm) {
            wx.switchTab({
              url: '../mine/mine'
            });
          }
        }
      });
    } else {
      wx.navigateTo({
        url: '../advise/advise',
      });
    }
  },

  // 居民诉求
  lost: function() {
    if (!this.data.login) {
      wx.showModal({
        title: '提示',
        content: '请先完成登录',
        success: (res) => {
          if (res.confirm) {
            wx.switchTab({
              url: '../mine/mine'
            });
          }
        }
      });
    } else {
      wx.navigateTo({
        url: '../complaint/complaint',
      });
    }
  },

  // 服务电话
  dianhua: function() {
    if (!this.data.login) {
      wx.showModal({
        title: '提示',
        content: '请先完成登录',
        success: (res) => {
          if (res.confirm) {
            wx.switchTab({
              url: '../mine/mine'
            });
          }
        }
      });
    } else {
      wx.navigateTo({
        url: '../dianhua/dianhua',
      });
    }
  },

  // 风采展示
  show: function() {
    if (!this.data.login) {
      wx.showModal({
        title: '提示',
        content: '请先完成登录',
        success: (res) => {
          if (res.confirm) {
            wx.switchTab({
              url: '../mine/mine'
            });
          }
        }
      });
    } else {
      wx.showModal({
        title: '敬请期待',
        content: '该功能尚在开发中，敬请期待！',
        showCancel: false,
        success: (res) => {
          if (res.confirm) {
            console.log('用户点击确定');
          }
        }
      });
    }
  },

  onReady: function() {},

  onLoad: function() {
    this.initMarquee();
  },

  onHide: function() {},

  onUnload: function() {},

  onPullDownRefresh: function() {},

  onReachBottom: function() {},

  onShareAppMessage: function() {}
});
